home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / XText / XTAction.m < prev    next >
Encoding:
Text File  |  1992-04-14  |  3.2 KB  |  180 lines

  1. /*    This file is part of the XText package (version 0.8)
  2.     Mike Dixon, April 1992
  3.     
  4.     Copyright (c) 1992 Xerox Corporation.  All rights reserved.
  5.  
  6.     Use and copying of this software and preparation of derivative works based
  7.     upon this software are permitted.  This software is made available AS IS,
  8.     and Xerox Corporation makes no warranty about the software or its
  9.     performance.
  10. */
  11.  
  12. #import "XTAction.h"
  13. #import "XText.h"
  14. #import "ErrorStream.h"
  15. #import <appkit/Application.h>
  16. #import <stdio.h>
  17. #import <string.h>
  18.  
  19. @implementation XTAction
  20.  
  21. static id undefined_action = 0;
  22.  
  23. + undefinedAction
  24. {
  25.     if (!undefined_action)
  26.         undefined_action = [[XTAction allocFromZone:[NXApp zone]] init];
  27.     return undefined_action;
  28. }
  29.  
  30. - applyTo:xtext event:(NXEvent *)event
  31. {
  32.     [xtext unboundKey];
  33.     return self;
  34. }
  35.  
  36. @end
  37.  
  38. @implementation XTMsg0Action
  39.  
  40. - initSel:(SEL)sel
  41. {
  42.     [super init];
  43.     action_sel = sel;
  44.     return self;
  45. }
  46.  
  47. - applyTo:xtext event:(NXEvent *)event
  48. {
  49.     return [xtext perform:action_sel];
  50. }
  51.  
  52. @end
  53.  
  54. @implementation XTMsg1Action
  55.  
  56. - initSel:(SEL)sel arg:(int)arg
  57. {
  58.     [super init];
  59.     action_sel = sel;
  60.     action_arg = arg;
  61.     return self;
  62. }
  63.  
  64. - applyTo: xtext event:(NXEvent *)event
  65. {
  66.     return [xtext perform:action_sel with:(id)action_arg];
  67. }
  68.  
  69. @end
  70.  
  71. @implementation XTMsg2Action
  72.  
  73. - initSel:(SEL)sel arg:(int)arg1 arg:(int)arg2
  74. {
  75.     [super init];
  76.     action_sel = sel;
  77.     action_arg1 = arg1;
  78.     action_arg2 = arg2;
  79.     return self;
  80. }
  81.  
  82. - applyTo: xtext event:(NXEvent *)event
  83. {
  84.     return [xtext perform:action_sel
  85.                     with:(id)action_arg1 with:(id)action_arg2];
  86. }
  87.  
  88. @end
  89.  
  90. @implementation XTDispatchAction
  91.  
  92. - initBase:(const char *)base estream:errs
  93. {
  94.     keyCode k;
  95.  
  96.     [super init];
  97.     for (k=0; k<KEY_CODES; ++k)
  98.         actions[k] = nil;
  99.     if (!strcmp(base,"none")) {}
  100. //    else if (!strcmp(base,"other_base"))
  101. //        initbase_other_base(actions)
  102.     else {
  103.         if ((*base != 0) && strcmp(base,"emacs")) {
  104.             char msg[100];
  105.             
  106.             sprintf(msg, "Unknown base '%.32s', using emacs instead", base);
  107.             [(errs ? errs : [ErrorStream default]) report:msg];
  108.         }
  109.         initbase_emacs(actions, [self zone]);
  110.     }
  111.     return self;
  112. }
  113.  
  114. - bindKey:(keyCode)key toAction:action estream:errs
  115. {
  116.     if ((key < 0) || (key >= KEY_CODES)) {
  117.         char msg[40];
  118.         sprintf(msg, "Invalid key code: %d", key);
  119.         [(errs ? errs : [ErrorStream default]) report:msg];
  120.     } else
  121.         actions[key] = action;
  122.     return self;
  123. }
  124.  
  125. - applyTo:xtext event:(NXEvent *)event
  126. {
  127.     keyCode k = (event->data.key.keyCode << 4);
  128.     id action;
  129.  
  130.     if ((k >= 0) && (k < KEY_CODES)) {
  131.         if (event->flags & NX_CONTROLMASK)   k += 1;
  132.         if (event->flags & NX_SHIFTMASK)     k += 2;
  133.         if (event->flags & NX_ALTERNATEMASK) k += 4;
  134.         if (event->flags & NX_COMMANDMASK)   k += 8;
  135.         action = actions[k];
  136.         if (action)
  137.             return [action applyTo:xtext event:event];
  138.     }
  139.     return nil;
  140. }
  141.  
  142. @end
  143.  
  144. @implementation XTEventMsgAction
  145.  
  146. - initSel:(SEL)sel
  147. {
  148.     [super init];
  149.     action_sel = sel;
  150.     return self;
  151. }
  152.  
  153. - applyTo:xtext event:(NXEvent *)event
  154. {
  155.     return [xtext perform:action_sel with:(id)event];
  156. }
  157.  
  158. @end
  159.  
  160. @implementation XTSeqAction
  161.  
  162. - initLength:(int)len actions:(XTAction **)acts
  163. {
  164.     [super init];
  165.     length = len;
  166.     actions = acts;
  167.     return self;
  168. }
  169.  
  170. - applyTo:xtext event:(NXEvent *)event
  171. {
  172.     int i;
  173.  
  174.     for (i=0; i<length; ++i)
  175.         [actions[i] applyTo:xtext event:event];
  176.     return self;
  177. }
  178.  
  179. @end
  180.